home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
BSFCOPY.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-08-04
|
6KB
|
193 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: bsfcopy.c
// Title: Base library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains .
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <bs.h>
//----------------------------------------------------------------------------
// Description: Copy a file
// Parameters: pcszSrc Name of source file.
// pcszDst Name of destination file.
// fs Copy options
// FCOPY_NO_NEWER Don't overwrite newer files
// FCOPY_BACKUP Create backup
// FCOPY_NO_DATE Don't preserve date.
// FCOPY_NO_OVERWRITE Don't overwrite existing.
// FCOPY_APPEND Append to existing
// pusAction If successful, one or more of the following.
// FCOPY_NEWER_EXISTS
// FCOPY_BACKUP_CREATED
// FCOPY_DST_EXISTS
// FCOPY_FILE_COPIED
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E CopyFile(PCSZ pcszSrc, PCSZ pcszDst, FLAG16 fs, PUSHORT pusAction)
{
CHAR szSrc[MAX_PATH];
CHAR szDst[MAX_PATH];
HF hfSrc = -1, hfDst = -1;
FLAG16 fsSrc = FL_OPEN|FL_READONLY|FL_DENYREADWRITE|FL_BINARY;
FLAG16 fsDst;
BOOL fResult = FALSE;
USHORT usAction = 0;
BS_FINFO finfoSrc, finfoDst;
FPOS fposDst;
Assert(pcszSrc && pcszDst);
strcpy(szSrc, pcszSrc);
strcpy(szDst, pcszDst);
FnameNormalize(szSrc, FNAME_DIR);
FnameNormalize(szDst, FNAME_DIR);
if (!FnameIsFile(szSrc))
{
Error("Could not copy from file.\n%s", szSrc);
goto ERROR_EXIT;
}
if (!FinfoGet(szSrc, &finfoSrc))
goto ERROR_EXIT;
if (FnameIsFile(szDst))
{
if (!FinfoGet(szDst, &finfoDst))
goto ERROR_EXIT;
usAction |= FCOPY_DST_EXISTS;
if (fs & FCOPY_NO_OVERWRITE) // Don't overwrite existing files
{
fResult = TRUE;
goto ERROR_EXIT;
}
if (finfoSrc.timet <= finfoDst.timet) // Check if existing file is
{ // more current
usAction |= FCOPY_NEWER_EXISTS;
if (fs & FCOPY_NO_NEWER)
{
fResult = TRUE;
goto ERROR_EXIT;
}
}
if (fs & FCOPY_BACKUP)
{
CHAR szBackup[MAX_PATH];
FnameAppendExt(strcpy(szBackup, szDst), ".BAK", TRUE);
if (FnameExists(szBackup))
if (!FnameDelete(szBackup))
goto ERROR_EXIT;
if (!FnameRename(szDst, szBackup))
goto ERROR_EXIT;
usAction |= FCOPY_BACKUP_CREATED;
}
}
if (!FileOpen(&hfSrc, szSrc, fsSrc, NULL))
goto ERROR_EXIT;
if (fs & FCOPY_APPEND)
fsDst = FL_OPEN|FL_CREATE|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
else
fsDst = FL_CREATE|FL_TRUNCATE|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
if (!FileOpen(&hfDst, szDst, fsDst, NULL))
goto ERROR_EXIT;
if (fs & FCOPY_APPEND)
fposDst = FileGetSize(hfDst); // Copy at end of file!!
else
fposDst = 0L;
if (!FileCopy(hfDst, fposDst, hfSrc, 0, (FPOS)-1L))
goto ERROR_EXIT;
FileClose(hfDst);
hfDst = -1;
if (!(fs & FCOPY_NO_DATE)) // Set date/time for copied file
if (!FinfoSet(szDst, &finfoSrc))
goto ERROR_EXIT;
if (BTEST(fs, FCOPY_RW))
{
if (!FinfoGet(szDst, &finfoDst))
goto ERROR_EXIT;
finfoDst.usAttrib &= ~FA_READONLY;
if (!FinfoSet(szDst, &finfoDst))
goto ERROR_EXIT;
}
usAction |= FCOPY_FILE_COPIED;
fResult = TRUE;
ERROR_EXIT:
if (pusAction)
pusAction[0] = usAction;
if (hfSrc >= 0)
FileClose(hfSrc);
if (hfDst >= 0)
FileClose(hfDst);
return fResult;
}
//----------------------------------------------------------------------------
// Description: Run standard test suite
// Parameters: sTest Test to run.
// 0 Run all default tests (except).
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
#if COMPILE_TEST
BOOL FN CopyTest(SHORT sTest)
{
NOTUSED(sTest);
if (!CopyFile("\\dev\\base\\bsfcopy.c", "\\dev\\base\\test.tmp", FCOPY_BACKUP, NULL))
return FALSE;
if (!CopyFile("\\dev\\base\\bsfcopy.c", "\\dev\\base\\test.tmp", FCOPY_BACKUP, NULL))
return FALSE;
if (!CopyFile("\\dev\\base\\bsfcopy.c", "\\dev\\base\\test.tmp", FCOPY_BACKUP|FCOPY_APPEND, NULL))
return FALSE;
return TRUE;
}
#endif
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------